/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShServiceDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShServiceService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShService;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShServiceService 实现类
 * 
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shServiceService")
public class ShServiceServiceImpl extends BaseServiceImpl<ShService, Long>
		implements ShServiceService {

	@Resource
	private ShServiceDao shServiceDao;

	public BaseDao<ShService, Long> getDao() {
		return shServiceDao;
	}

	@Override
	public DataGrid queryShServices(PageHelper page, ShService service) {
		DataGrid dataGrid = new DataGrid();
		long totalCount = shServiceDao.selectEntryListCount(service);
		if (!StringUtils.isBlank(page.getOrder())) {
			service.setOrderFieldType(page.getOrder());
		}
		if (!StringUtils.isBlank(page.getSort())) {
			service.setOrderField(page.getSort());
		}
		dataGrid.setTotal(totalCount);
		int startIndex = (page.getPage() - 1) * page.getRows();
		int endIndex = page.getPage() * page.getRows();
		if (totalCount > 0 && totalCount > startIndex) {
			service.setStartIndex(startIndex);
			service.setEndIndex(endIndex);
			List<ShService> shServices = shServiceDao
					.selectShServices(service);
			dataGrid.setRows(shServices);
		}
		return dataGrid;
	}

	public Map<String, Object> getStatisticsData(ShService shService) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<Object> serviceAndFeedbackList = new ArrayList<Object>();
		List<Object> serviceTypeList = new ArrayList<Object>();

		Map<String, Object> serviceAndFeedbackCount = shServiceDao
				.queryServiceAndFeedbackCount(shService);
		Map<String, Object> serviceTypeCount = shServiceDao
				.queryServiceTypeCount(shService);

		Set<String> serviceAndFeedbackSet = serviceAndFeedbackCount.keySet();
		Iterator<String> serviceAndFeedbackIter = serviceAndFeedbackSet
				.iterator();
		while (serviceAndFeedbackIter.hasNext()) {
			Object serviceAndFeedback = serviceAndFeedbackCount
					.get(serviceAndFeedbackIter.next());
			serviceAndFeedbackList.add(serviceAndFeedback);
		}

		Set<String> serviceTypeSet = serviceTypeCount.keySet();
		Iterator<String> serviceTypeIter = serviceTypeSet.iterator();
		while (serviceTypeIter.hasNext()) {
			Object serviceType = serviceTypeCount.get(serviceTypeIter.next());
			serviceTypeList.add(serviceType);
		}

		result.put("serviceAndFeedbackCount", serviceAndFeedbackList);
		result.put("serviceTypeCount", serviceTypeList);
		return result;
	}

	@Override
	public List<ShService> getPropertyServiceList(ShService shService) {
		return shServiceDao.queryShServices(shService);
	}

	@Override
	public int count(ShService shService) {
		return shServiceDao.selectEntryListCount(shService);
	}

	@Override
	public Long queryServiceCount(ShService queryShService) {
		return shServiceDao.queryServiceCount(queryShService);
	}
	
	
	

}